/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.dataTypePropGen;

import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.property.InterfacePropertyGenerator;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.api.attr.EnumDVAttribute;
import com.inspur.edp.cef.api.attr.EnumDVStrAttribute;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.EnumIndexType;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.cef.designtime.api.element.GspEnumValue;
import com.inspur.edp.cef.entity.dependenceTemp.DataValidator;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.DataTypeDataGeneratorContext;
import com.inspur.edp.cef.generator.overall.Utils;
import lombok.var;
import org.eclipse.jdt.core.dom.StringLiteral;

import java.util.ArrayList;

public abstract class BaseDataPropertyGenerator extends InterfacePropertyGenerator {
  protected IGspCommonField field;
  protected DataTypeDataGeneratorContext dataGenContext;
  private boolean convertToUpper = false;
  protected BaseDataPropertyGenerator(
          IGspCommonField field, DataTypeDataGeneratorContext dataGenContext) {
    this(field, dataGenContext, false);
  }

  protected BaseDataPropertyGenerator(
          IGspCommonField field, DataTypeDataGeneratorContext dataGenContext, boolean toUpper) {
    this.field = field;
    this.dataGenContext = dataGenContext;
    this.convertToUpper = toUpper;
  }

  @Override
  protected final String getPropertyName() {
    DataValidator.checkForEmptyString(field.getLabelID(), "element.getLabelID()");
    if(convertToUpper){
      return Utils.toUpperCaseFirstOne(field.getLabelID());
    }
    else {
      return field.getLabelID();
    }
  }

  @Override
  protected ArrayList<AnnotationInfo> getGetterAttributeList() {
    if(convertToUpper)
      return null;
    var list =super.getGetterAttributeList();
    if(list == null)
        list = new ArrayList<AnnotationInfo>();
    addPropertyGetAttributeInfo(list);
    if (field.getObjectType() == GspElementObjectType.Enum) {
      if(field.getEnumIndexType()==EnumIndexType.String){
        addEnumStrAttributeInfo(list);
      }
      else {
        addEnumAttributeInfo(list);
      }
    }
    return list;
  }

  private void addPropertyGetAttributeInfo(ArrayList<AnnotationInfo> list) {

//    AnnotationInfo annotationInfo = new AnnotationInfo();
//    //TODO: caf暂不支持, 主动加import
//    dataGenContext.getDataTypeGeneratorContext().getEntityGenContext().addImport(PropertyGetAtrribute.class.getName());
//    TypeInfo typeInfo = new TypeInfo(PropertyGetAtrribute.class);
//    annotationInfo.setTypeInfo(typeInfo);
//    annotationInfo.setAnnotationType(AnnotationType.Normal);
//    annotationInfo.getParams().put("propName", ExpressionUtils.getStringLiteral(ast, field.getLabelID()));
//    list.add(annotationInfo);
  }

  private void addEnumAttributeInfo(ArrayList<AnnotationInfo> list) {
    int defaultValue = 0;
    for (GspEnumValue item : field.getContainEnumValues()) {
      if (item.getIsDefaultEnum()) {
        defaultValue = item.getIndex();
        break;
      }
    }

    AnnotationInfo annotationInfo = new AnnotationInfo();
    annotationInfo.setAnnotationType(AnnotationType.SingleMember);
    //TODO: caf暂不支持, 主动加import
    dataGenContext.getDataTypeGeneratorContext().getEntityGenContext().addImport(EnumDVAttribute.class.getName());
    var typeInfo = new TypeInfo(EnumDVAttribute.class);
    annotationInfo.setTypeInfo(typeInfo);
    annotationInfo.setAnnotationType(AnnotationType.Normal);
    annotationInfo.getParams().put("defaultValue", ast.newNumberLiteral(String.valueOf(defaultValue)));
    list.add(annotationInfo);
  }
  private void addEnumStrAttributeInfo(ArrayList<AnnotationInfo> list) {
    String strDefaultValue = "";
    for (GspEnumValue item : field.getContainEnumValues()) {
      if (item.getIsDefaultEnum()) {
        strDefaultValue = item.getStringIndex();
        break;
      }
    }

    AnnotationInfo annotationInfo = new AnnotationInfo();
    annotationInfo.setAnnotationType(AnnotationType.SingleMember);
    //TODO: caf暂不支持, 主动加import
    dataGenContext.getDataTypeGeneratorContext().getEntityGenContext().addImport(EnumDVStrAttribute.class.getName());
    var typeInfo = new TypeInfo(EnumDVStrAttribute.class);
    annotationInfo.setTypeInfo(typeInfo);
    annotationInfo.setAnnotationType(AnnotationType.Normal);
    StringLiteral stringLiteral= ast.newStringLiteral();
    stringLiteral.setLiteralValue(strDefaultValue);
    annotationInfo.getParams().put("strDefaultValue", stringLiteral);
    list.add(annotationInfo);
  }
  @Override
  protected String getFieldName() {
    return null;
  }

  protected MediatePropertyInfo propInfo;
  @Override
  protected void beforeGenerate() {
    super.beforeGenerate();
    TypeInfo t = this.getPropertyType();
    if(convertToUpper==false)
      dataGenContext.getPropertyInfos().put(getPropertyName(),
          propInfo = new MediatePropertyInfo(getPropertyName()) {{
            setElement(field);
            setPropertyType(t);
          }}
      );
  }
}
